// pages/integralactivities/integralactivities.js
var app = getApp();
const util = require('../../utils/util.js');
var GlobalData = require('../../utils/GlobalData.js');
var dataManager = require('../../utils/dataManager.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shan: "deng1",
    isShan: false,
    recordid: 0,
    IDcard: '',
    mobile: '',
    isWeiZhong: false,
    /**省 */
    province: null,
    city: '',
    area: '',
    detailedAddress: '',
    /**显示协议 */
    ShowAgreement: false,
    /**显示对勾 */
    isAgreed: false,
    /**收货地址不可点击 */
    isClickAddress: false,

    //input数据
    input: {
      mobile: null,
      realName: "",
      IDcard: "",
      province: "",
      city: "",
      area: "",
      detailedAddress: "",

    },
    timer: null,	//setTimeout的ID，用clearTimeout清除
    timershan: null,	//setTimeout的ID，用clearTimeout清除
    prize: -1,	//中奖位置
    /**奖品图片 */
    prizePic: '',
    //抽奖次数
    lotteryCount: 0,
    /**是否点击 */
    isClick: true,
    /**奖品数组 */
    prizeArr: GlobalData.prizeArr,
    /**获奖记录 */
    winRecord: [],
    /**显示中奖 */
    isShow: false,
    /**提示实名认证 */
    isCertification: false,
    /**填写信息 */
    msg: false,
    /**没有抽奖次数的弹框 */
    NoCount: false,
    /**奖品类型 2为实物 */
    type: 0,
    hongBao: false,
    /**状态数组 */
    stateArr: [false, false, false, false, false, false, false, false],
    /**活动是否结束 */
    activetimeEnd: false,
    /**积分数 */
    integral: 0,
    top: "",
    timerSpeed: 0,
    timerCount: [],
    reduceSpeed: [],
    arr: [],
    sum: 0,
    bgmsg: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  getLuckyDrawprizeConfigComplete() {
    this.setData({
      prizeArr: GlobalData.prizeArr,
    })
  },
  onShow: function () {
    var that = this;
    // that.data.timershan = setInterval(() => {
    //   that.data.isShan = !that.data.isShan;
    //   let tu = that.data.isShan ? "deng1" : "deng2"
    //   that.setData({
    //     shan: tu
    //   })
    // }, 1000);


    if (!this.data.prizeArr.length) {
      dataManager.rigisterObserver("getluckydrawprizeconfig", this.getLuckyDrawprizeConfigComplete, this);
      dataManager.getLuckyDrawprizeConfig();
    }

    // if (this.data.top == "addressmanage") {
    if (GlobalData.prixzeAddress) {
      this.data.msg = true;
      this.data.input.realName = GlobalData.prixzeAddress.name;
      this.data.input.mobile = GlobalData.prixzeAddress.phone;
      // this.input.detailedAddress = GlobalData.prixzeAddress.address;
      this.data.input.city = GlobalData.prixzeAddress.areas + GlobalData.prixzeAddress.address;
      // this.input.area = GlobalData.prixzeAddress.areas;
      this.setData({
        msg: this.data.msg,
        input: this.data.input,
      })
    }
    // }
    if (GlobalData.homeData) {
      this.data.integral = GlobalData.homeData.bonuspoints;
      this.setData({
        integral: this.data.integral
      })
    }
    for (let i = 0; i < 12; i++) {
      this.data.reduceSpeed[i] = Math.floor(i / 4 + 2);
    }
    if (GlobalData.inteWinRecord.length) {
      this.data.winRecord = GlobalData.inteWinRecord[1];
      for (let i = 0; i < this.data.winRecord.length; i++) {
        this.data.winRecord[i].createtime = this.data.winRecord[i].createtime.replace("T", " ")
      }
      this.setData({
        winRecord: this.data.winRecord
      })
    }
  },
  /**
    * 点击抽奖 
    */
  onLottery() {
    if (this.data.isClick) {
      if (GlobalData.homeData.bonuspoints < 10) {
        app.onshowModal("积分不足");
        return;
      }
      this.data.isClick = false;
      this.onRoll();
    }
  },

  /**抽奖 */
  onRoll() {
    try {
      let url = { method: "luckydraw", Token: app.getToken() };
      dataManager.OnRequestPost(url, this.luckydrawComplete);
    } catch (error) {
      console.log(error)
    }
  },
  luckydrawComplete(data) {
    this.data.prize = data["prizeid"];
    GlobalData.recordid = data.recordid;
    this.data.type = data["type"];
    if (this.data.type == 1) {
      this.data.isShow = true
    } else {
      this.data.isShow = false;
    }
    let index = (this.data.prize % 10 - 1);
    this.data.prize = index;
    this.onChange();
    this.data.prizePic = data.picture;
    this.data.integral -= 10;
    this.setData({
      type: this.data.type,
      isShow: this.data.isShow,
      prizePic: this.data.prizePic,
      integral: this.data.integral,
    })
  },
  onGetHomeDataComplete() {
    this.data.integral = GlobalData.homeData.bonuspoints;
    this.setData({
      integral: this.data.integral,
    })

  },
  //转动
  onChange() {
    var count = 0;
    var countTrue = 0;
    for (let i = 0; i < this.data.stateArr.length; i++) {
      if (this.data.stateArr[i]) {
        count = ((this.data.prize - i) >= 0) ? this.data.prize - i : this.data.prize - i + 8;
      } else {
        countTrue += 1;
      }
    }
    if (countTrue == this.data.stateArr.length) {
      this.data.stateArr[0] = true;
      count = this.data.prize;
    }
    this.data.timerCount.splice(0, this.data.timerCount.length);
    for (let i = 0; i < (20 + count); i++) {
      this.data.timerCount[i] = 1;
    }
    this.data.arr = this.data.timerCount.concat(this.data.reduceSpeed);
    this.data.timer = setInterval(() => {
      if (this.data.sum == this.data.arr.length) {
        clearInterval(this.data.timer);
        setTimeout(() => {
          if (this.data.type == 5) {
            this.data.isWeiZhong = true;
            this.setData({
              isWeiZhong: this.data.isWeiZhong,
            })
          } else {
            this.data.bgmsg = true;
            this.setData({
              bgmsg: this.data.bgmsg,
            })
          }


          // this.isShow = true;
        }, 1050);
        // this.isClick = true;
        this.data.sum = 0;
        this.setData({
          isWeiZhong: this.data.isWeiZhong,
          bgmsg: this.data.bgmsg,
          stateArr: this.data.stateArr
        })
        return;
      }
      if ((this.data.timerSpeed > this.data.arr[this.data.sum])) {
        this.data.timerSpeed = 0;
        this.data.sum++;
        for (var i = 0; i < this.data.stateArr.length; i++) {
          if (this.data.stateArr[i]) {
            this.data.stateArr[i] = false;
            var num = i;
            num = (++num >= 8) ? 0 : num;
            this.data.stateArr[num] = true;

            break;
          }
        }
        this.setData({
          stateArr: this.data.stateArr
        })
      }
      this.data.timerSpeed += 1;
    }, 33);
  },

  ngAfterViewInit() {
    this.renderer.setStyle(this.bgmsg.nativeElement, "display", "none");
    //闪灯效果
    var num = 0;
    $(".shanDeng").attr("class", function () {
      setInterval(function () {
        num++;
        if (num % 2 == 0) {
          $('#deng').addClass("shanDeng2");
        } else {
          $('#deng').addClass("shanDeng");
          $('#deng').removeClass('shanDeng2');
        }
      }, 1000)
    })
    /**中奖记录轮播效果 */
    this.lotteryLun();
  },
  /**中奖记录轮播效果 */
  lotteryLun() {
    jQuery(".txtScroll-top").slide({
      titCell: ".hd li",
      mainCell: ".bd ",
      autoPage: true,
      effect: "topLoop",
      autoPlay: true,
      vis: this.winRecord.length,
      easing: "easeOutCirc"
    });
    // console.log("中奖记录轮播效果", this.winRecord.length);
  },

  /**详细规则 */
  onRules() {
    dataManager.UseNavigateTo("/pages/lotteryrules/lotteryrules", false)
  },
  /**中奖记录 */
  onRecord() {
    dataManager.UseNavigateTo("/pages/integrallotteryrecord/integrallotteryrecord", false)
  },
  /**关闭未中奖提示 */
  againLottery() {
    this.data.isClick = true;
    this.data.isWeiZhong = false;
    this.setData({
      isWeiZhong: this.data.isWeiZhong
    })
  },
  /**提示中奖 */
  onReceive() {
    this.data.isClick = true;
    this.setData({
      bgmsg: false
    })
    // this.renderer.setStyle(this.bgmsg.nativeElement, "display", "none");
    // this.type = 2;
    if (this.data.type == 2) {
      this.data.isCertification = true;
      this.setData({
        isCertification: this.data.isCertification
      })
    }
    dataManager.rigisterObserver("home", this.onGetHomeDataComplete, this)
    dataManager.getHomeData();
  },
  /**关闭提示填写地址，填写地址弹框 */
  onClose() {
    this.data.isCertification = false;
    this.data.msg = false;
    this.data.bgmsg = false;
    GlobalData.prixzeAddress = null;
    // this.renderer.setStyle(this.bgmsg.nativeElement, "display", "none");
    this.setData({
      isCertification: this.data.isCertification,
      msg: this.data.msg,
      bgmsg: this.data.bgmsg,
    })

  },
  /**关闭没有抽奖次数的弹框 */
  onCloseChou() {
    this.data.NoCount = false;
  },
  /**立即购卡 */
  onBuyCard() {
    // this.router.navigateByUrl("buycard/" + GlobalData._random());
  },
  /**提示实名认证 */
  onCertification() {
    this.data.isCertification = false;
    this.setData({
      isCertification: this.data.isCertification,
    })
    dataManager.UseNavigateTo("/pages/addressmanage/addressmanage?url=integralactivities", false);

  },
  /**确认填写地址 */
  onInformation() {
    if (!this.data.isAgreed) {
      return;
    }

    if (this.data.input.realName == "" || this.data.input.realName == null) {
      app.onshowModal("请输入您的名字");
      return;
    }
    if (this.data.input.mobile == "" || this.data.input.mobile == null) {
      app.onshowModal("请输入正确的手机号码");
      return;
    }
    if (this.data.input.city == "" || this.data.input.city == null) {
      app.onshowModal("请输入收货的省份，城市，地区");
      return;
    }
    var address = this.data.input.city;
    /**中奖商品id*/
    let recordid = this.data.recordid;
    // address = "四川省成都市青羊区世贸中心";

    try {
      let url = { method: "updatedrawrecordconsignee", Token: app.getToken(), "recordid": GlobalData.recordid, "consigneename": this.data.input.realName, "consigneeaddress": address, "consigneemobile": this.data.input.mobile };
      dataManager.OnRequestPost(url, this.updateDrawRecordConsigneeComplete);
    } catch (error) {
      console.log(error)
    }
  },
  updateDrawRecordConsigneeComplete() {
    this.data.msg = false;
    var that = this;
    wx.showModal({
      title: '提示',
      content: "添加地址成功。",
      showCancel: false,
      success: function (res) {
        that.setData({
          msg: that.data.msg
        })
        GlobalData.prixzeAddress = null;
      }
    })


  },
  /**关闭协议提示 */
  onCloseAgreement() {
    this.data.ShowAgreement = false;
    this.setData({
      ShowAgreement: this.data.ShowAgreement
    })
  },
  /**显示对勾或关闭 */
  onCloseGou() {
    this.data.isAgreed = !this.data.isAgreed;
    this.setData({
      isAgreed: this.data.isAgreed
    })
  },

  /**查看协议信息 */
  onCheckText() {
    this.data.ShowAgreement = true;
    this.setData({
      ShowAgreement: this.data.ShowAgreement
    })
  },
  /** 不同意*/
  onDisagreed() {
    this.data.ShowAgreement = false;
    this.data.isAgreed = false;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
      isAgreed: this.data.isAgreed,

    })
  },
  /** 同意协议0 */
  onAgreed() {
    this.data.ShowAgreement = false;
    this.data.isAgreed = true;
    this.setData({
      ShowAgreement: this.data.ShowAgreement,
      isAgreed: this.data.isAgreed,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(this.data.timershan);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.data.timershan);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})